package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.MovementApi;
import com.tanhua.api.UserInfoApi;
import com.tanhua.api.VideoApi;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManagerService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    //查询用户列表
    public PageResult<UserInfo> findAllUsers(Integer page, Integer pagesize) {
        //1.调用api实现分页查询
        Page<UserInfo> pages =userInfoApi.findByPage(page,pagesize);

        //设置用户的冻结数据
        for (UserInfo userInfo : pages.getRecords()) {
            if (redisTemplate.hasKey(Constants.USER_FREEZE +userInfo.getId())){
                userInfo.setUserStatus("2");
            }
        }
        //2.封装分页对象,返回数据
        PageResult pageResult = new PageResult(page,pagesize, (int) pages.getTotal(),pages.getRecords());
        return pageResult;
    }


    //查看用户详情
    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        if (redisTemplate.hasKey(Constants.USER_FREEZE +userInfo.getId())){
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }


    //查询指定用户发布的所有视频列表
    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
        //1.根据用户id分页查询小视频数据
        PageResult pageResult =  videoApi.findPageByUid(uid,page,pagesize);

        //2.判断查询结果是否为空,如果为空直接返回
        if (CollUtil.isEmpty(pageResult.getItems())) {
            return new PageResult(page,pagesize,0,null);

        }
        //3.查询用户的详情
        UserInfo userInfo = userInfoApi.findById(uid);
        //4.封装vos
        List<VideoVo> vos = new ArrayList<>();
        List<Video> videoList = pageResult.getItems();
        for (Video video : videoList) {
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        //5.封装分页对象 返回结果
        return new PageResult<>(page,pagesize,pageResult.getCounts(),vos);
    }


    //根据用户id和审核状态,分页查询动态数据
    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        //1.根据用户id和审核状态,分页查询动态数据'
        PageResult pageResult = movementApi.findMovementsByUid(uid,state,page,pagesize);
        //2.判断查询结果是否为空,为空直接返回
        if (CollUtil.isEmpty(pageResult.getItems())){
            return new PageResult<>(page,pagesize,0,null);
        }
        //3.查询用户详情
        UserInfo userInfo = userInfoApi.findById(uid);
        //4.封装vos
        List<MovementsVo> vos = new ArrayList<>();
        List<Movement> movementList = pageResult.getItems();
        for (Movement movement : movementList) {
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        //5.封装分页对象,返回结果
        return new PageResult<>(page,pagesize,pageResult.getCounts(),vos);
    }


    //用户冻结
    public Map userFreeze(Map params) {
        //1.获取请求参数
        String userId = params.get("userId").toString();
        String freezingTime = params.get("freezingTime").toString();

        //2.获取冻结的失效时间  单位是天 冻结时间: 1为冻结3天  2为冻结7天  3为永久冻结
        int day = 0;
        if ("1".equals(freezingTime)){
            day = 3;
        }else if ("2".equals(freezingTime)){
            day = 7;
        }else if ("3".equals(freezingTime)){
            day = -1;
        }

        //3.冻结数据保存到redis中  把冻结的数据转为JSON 保存到redis中
        String json = JSON.toJSONString(params);
        redisTemplate.opsForValue().set(Constants.USER_FREEZE +userId,json, Duration.ofDays(day));

        //4.返回结果
        Map retMap = new HashMap();
        retMap.put("message","用户冻结成功");
        return retMap;

    }


    //用户解冻
    public Map userUnfreeze(Map params) {
        //1.获取参数
        String userId = params.get("userId").toString();

        //2.删除冻结数据
        redisTemplate.delete(Constants.USER_FREEZE  +userId);
        //3.返回结果
        Map retMap = new HashMap();
        retMap.put("message","用户解冻成功");
        return retMap;
    }
}
